package com.itheima.book.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.book.mapper.CategoryMapper;
import com.itheima.book.service.CategoryService;
import com.itheima.book.utils.CategoryTreeBuilder;
import com.itheima.model.book.pojos.Category;
import com.itheima.model.book.vos.CategoryTreeVo;
import com.itheima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    /**
     * 获取分类树
     * @return
     */
    @Override
    @Cacheable(value = "category", key = "'tree'", unless = "#result == null")
    public ResponseResult getCategoryTree() {
        //查找所有可用的分类
        List<Category> categoryList = list(Wrappers.<Category>lambdaQuery().eq(Category::getIsEnabled, Category.ENABLE));
        List<CategoryTreeVo> vos = CategoryTreeBuilder.buildTree(categoryList);
        return ResponseResult.okResult(vos);
    }
}
